# Application-specific exceptions which auto-log the error and stack trace

class AnExceptionalBanana < StandardError
  # auto-log exception  
  def initialize(msg)
    super(msg)
    # trace doesn't exist here so we need to build it from Kernel.caller
    trace = self.class.to_s + ": " + msg + "\n"
    if Kernel.caller
      t = Kernel.caller
      # first 2 items in the stack are this initialize and the raise that called it
      trace << t.last(t.count - 2).join("\n") + "\n\n"
    end
    # TODO: error handling
    File.open(File.dirname(__FILE__) + '/log/error.log', 'a') {|f| f.write(trace)}
  end
  
end

# application-specific exceptions
class APersistentBananaException < AnExceptionalBanana; end
class AClockworkBananaException < AnExceptionalBanana; end
class AModelBananaException < AnExceptionalBanana; end